package com.carkeeper.shop.service.impl;

import com.carkeeper.pojo.ServiceType;
import com.carkeeper.shop.check.dao.TypeMapper;
import com.carkeeper.shop.service.TypeService;
import com.carkeeper.utile.IdWorker;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private IdWorker idWorker;

    /**
     * 增加服务分类
     * @param serviceType
     */
    @Override
    public void addServiceType(ServiceType serviceType) {
        if (serviceType!=null){

            String code = serviceType.getCode();
            //获取所有的分类数据
            List<ServiceType> typeList = typeMapper.findAllType();

            if (typeList.size()==0){//判断是否是第一条数据
                code=code+1000010000;
            }else {
                //获取最后一个数据
                ServiceType lastServiceType = typeList.get(typeList.size() - 1);
                //正则表达式获取code中的数字
                Pattern pattern = Pattern.compile("[^0-9]");
                Matcher matcher = pattern.matcher(lastServiceType.getCode());
                String strNum = matcher.replaceAll("");
                int number = Integer.parseInt(strNum);
                //组成新的code
                code = code+(number+1);
            }

            serviceType.setCode(code);

            serviceType.setId(String.valueOf(idWorker.nextId()));
            System.out.println(idWorker.nextId());
            typeMapper.insert(serviceType);
        }
    }

    /**
     * 修改服务分类
     * @param serviceType
     */
    @Override
    public void updateServiceType(ServiceType serviceType) {
        if (serviceType!=null){
            typeMapper.updateByPrimaryKeySelective(serviceType);
        }
    }

    /**
     * 查询所有的服务分类
     * @return
     */
    @Override
    public Page<ServiceType> findTypePage(int page,int size) {
        PageHelper.startPage(page, size);
        Page<ServiceType> serviceTypePage = (Page<ServiceType>)typeMapper.findAllType();
        if (serviceTypePage!=null && !serviceTypePage.isEmpty()) {
            return serviceTypePage;
        }else {
            return null;
        }
    }

    /**
     * 多条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<ServiceType> findTypePage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<ServiceType>)typeMapper.selectByExample(example);
    }

    /**
     * 删除服务分类
     * @param id
     */
    @Override
    public void deleteTypeById(String id) {
        Integer countByTypeId = typeMapper.findCountByTypeId(id);
        if (countByTypeId>0) {//表示有子分类
            throw new RuntimeException("无法删除");
        }
        typeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建快速查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(ServiceType.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap!=null){
            //服务分类编码
            if (searchMap.get("code")!=null&&!"".equals(searchMap.get("code"))) {
                criteria.andEqualTo("code",searchMap.get("code"));
            }
            //服务分类名称
            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))) {
                criteria.andEqualTo("name",searchMap.get("name"));
            }
        }
        return example;
    }


}
